package com.imooc.router.gradle

import com.android.build.api.transform.Transform
import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import groovy.json.JsonSlurper

class RouterPlugin implements Plugin<Project> {


    void apply(Project project) {

        if (project.plugins.hasPlugin(AppPlugin)) {
            AppExtension appExtension = project.extensions.getByType(AppExtension)
            Transform transform = new RouterMappingTransform()
            appExtension.registerTransform(transform)
        }

        if (project.extensions.findByName("kapt") != null) {
            project.extensions.findByName("kapt").arguments {
                arg("root_project_dir", project.rootProject.projectDir.absolutePath)
            }
        }

        project.clean.doFirst {
            File routerMapping = new File(project.rootProject.projectDir.absolutePath, "router_mapping")
            if (routerMapping.exists()) {
                routerMapping.deleteDir()
            }
        }
        if (!project.plugins.hasPlugin(AppPlugin)) {
            return
        }
        println("i am RouterPlugin,apply method is be called ${project.name}")
        project.getExtensions().create("router", RouterExtension)
        project.afterEvaluate {
            RouterExtension extension = project["router"]
            println("user setting wikidir = ${extension.wikiDir}")
            //task:compileReleaseJavaWithJavac
            project.tasks.findAll { task ->
                task.name.startsWith("compile") && task.name.endsWith("JavaWithJavac")
            }.each { task ->
                task.doLast {
                    File routerMapping = new File(project.rootProject.projectDir, "router_mapping")
                    if (!routerMapping.exists()) {
                        return
                    }
                    File[] allChildFiles = routerMapping.listFiles()
                    if (allChildFiles.length < 1) {
                        return
                    }
                    StringBuilder stringBuilder = new StringBuilder()
                    stringBuilder.append("# 页面文档\n\n")
                    allChildFiles.each { child ->
                        if (child.name.endsWith(".json")) {
                            JsonSlurper jsonSlurper = new JsonSlurper()
                            def content = jsonSlurper.parse(child)
                            content.each { innerContent ->
                                def url = innerContent['url']
                                def description = innerContent['description']
                                def realPath = innerContent['realPath']
                                stringBuilder.append("##$description")
                                stringBuilder.append("-url$url\n")
                                stringBuilder.append("-realPath$realPath\n")
                                stringBuilder.append("\n\n")
                            }
                        }
                    }

                    File wikiFileDir = new File(extension.wikiDir)
                    if (!wikiFileDir.exists()) {
                        wikiFileDir.mkdir()
                    }
                    File wikiFile = new File(wikiFileDir, "luyou.md")
                    if (wikiFile.exists()) {
                        wikiFile.delete()
                    }
                    wikiFile.write(stringBuilder.toString())
                }
            }
        }
    }
}